<?php

class EnderecoModel extends AppModel {

	protected $_tableName = "endereco";
	
	public static function fullAddress($id,$breakLine=" \n") {
		
		$enderecoModel = new EnderecoModel();
		$endereco = $enderecoModel->findById($id);
		
		$response  = $endereco["logradouro"] . ", " . $endereco["numero"] . ", ";
		$response .= ($endereco["complemento"] != "") ? $endereco["complemento"] . ", " : ""; 
		$response .= $endereco["bairro"] . $breakLine;
		$response .= Mask::maskNumber( $endereco["cep"], Mask::$CEP ) . $breakLine;
		$response .= $endereco["cidade"] . "/" . $endereco["estado"];
		
		return $response;
		
	}
	
	public static function findByCondominio($id) {
		
		$sql = "SELECT endereco.* FROM endereco INNER JOIN condominio ON endereco.id=condominio.endereco_id WHERE condominio.id='".$id."'";
		$rs = Model::executeSQL($sql);
		$endereco = mysql_fetch_assoc($rs);
		return $endereco;
		
	}
	
	/*/public static function findAllCondominios() {
		
		$sql = "SELECT endereco.id AS endereco_id, condominio.nome AS condominio_nome FROM endereco INNER JOIN condominio ON endereco.id=condominio.endereco_id";
		$rs = Model::executeSQL($sql);
		$enderecos = array();
		while ( ($tmp = mysql_fetch_assoc($rs)) != null)
			$enderecos[] = $tmp;
		return $enderecos;
		
	}
	
	public static function condominiosConvertToSelectArray($enderecos_condominios ,$valueSelected="") {
		$response = array();
		foreach ($enderecos_condominios as $endereco_condominio){
			$tmp = array();
			$tmp[ "option.value" ] = Encryption::encode($endereco_condominio['endereco_id']);
			if( $endereco_condominio['endereco_id'] == $valueSelected )
				$tmp[ "option.selected" ] = "selected";
			$tmp[ "option.label" ] = $endereco_condominio['condominio_nome'];
			$response[] = $tmp;
		}
		return $response;
	}//*/
	
}

?>